package com.ddys.triemap;

/**
 * Created with IntelliJ IDEA.
 * Author <a href="mailto:xiaowenke@zto.cn">xwk</a>
 * Date: 2016-08-25
 * Time: 16:00
 * To change this template use File | Settings | File Templates.
 */
public class Criteria<T> {

    enum STATUS {
         FILTERED, HIT, DONE
    }
    private int start;
    private int end;
    private int index;
    private IFilter<T> filter;

    public Criteria(int pageNo, int pageCount) {
        this.init(null,pageNo,pageCount);
    }
    public Criteria(IFilter<T> filter,int pageNo, int pageCount) {
        this.init(filter,pageNo,pageCount);

    }
    public Criteria(IFilter<T> filter) {
        this.filter= filter;
        this.start=0;
        this.end = Integer.MAX_VALUE;
    }
    public void init(IFilter<T> filter,int pageNo, int pageCount){
        start = (pageNo - 1) * pageCount;
        end = pageNo * pageCount;
        this.filter=filter;
    }

    public IFilter<T> getFilter() {
        return filter;
    }


    public STATUS next(T value) {
        if(filter!=null&&!filter.fileter(value)){
            return STATUS.FILTERED;
        }
        index++;
        if (index > end) {
            return STATUS.DONE;
        } else if (index < start) {
            return STATUS.FILTERED;
        }
        return STATUS.HIT;
    }

}
